package com.buercorp.wangyu.core1.ch12.threads.threads;

import com.buercorp.wangyu.core1.ch12.threads.threads.Bank;

import java.lang.annotation.Target;

/**
 * @author Cay Horstmann
 * @version 1.30 2004-08-01
 */
public class ThreadTest {

   public static final int DELAY = 10;
   public static final int STEPS = 100;
   public static final double MAX_AMOUNT = 1000;

   private static Runnable task3() {

      return null;
   }

   private static void task3(Runnable runnable) {

   }

   public static void main(String[] args) {
      var bank = new Bank(4, 100000);

      task3(new Runnable() {
         @Override
         public void run() {
         }
      });

      // Runnable接口 run方法
      Runnable task1 = () ->
      {
         try {
            for (int i = 0; i < STEPS; i++) {
               double amount = MAX_AMOUNT * Math.random();
               // 重新计算总金额
               bank.transfer(0, 1, amount);
               // 休眠指定毫秒数
               Thread.sleep((int) (DELAY * Math.random()));
            }
         } catch (InterruptedException e) {
         }
      };


      Runnable task2 = () ->
      {
         try {
            for (int i = 0; i < STEPS; i++) {
               double amount = MAX_AMOUNT * Math.random();
               // 计算总金额 转移
               bank.transfer(2, 3, amount);
               Thread.sleep((int) (DELAY * Math.random()));
            }
         } catch (InterruptedException e) {
         }
      };
      // new Thread(task1) 新建状态  start() 启动线程
      new Thread(task1).start();

      new Thread(task2).start();
   }

}
